package com.tongzhou.client;

import java.util.List;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.link.dao.ILinkDAO;
import com.tongzhou.tongzhou.link.dao.IProductScheduleDAO;
import com.tongzhou.tongzhou.link.dao.impl.LinkDAO;
import com.tongzhou.tongzhou.link.dao.impl.ProductScheduleDAO;
import com.tongzhou.tongzhou.link.model.Degree;
import com.tongzhou.tongzhou.link.model.Link;
import com.tongzhou.tongzhou.link.model.ProductSchedule;

//生产排期
public class ClientProduct {
	ILinkDAO dao=new LinkDAO();
	IProductScheduleDAO proDAO=new ProductScheduleDAO();
	public String getInfo(String json){
		JSONObject jsonObject = JSONObject.fromObject( json );
		String time=null;
		if(jsonObject.get("time")!=null){
			time=(String) jsonObject.get("time");
		}
		
		StringBuffer str=new StringBuffer();
		if(time==null){
			return "{'success':0,'msg':'输入参数有误!'}";
		}else{
			try {
				ProductSchedule pro=proDAO.getProductScheduleByTime(time);
				if(pro==null){
					str.append("{'success':0,'msg':'没有对应的生产排期!'}");
				}else{
					List<Link> links;

					links = dao.getLinktByProId(pro.getId());
					str.append("{'success':1,'msg':[");
					if(links.size()>0){
						for(int i=0;i<links.size();i++){
							Link link=links.get(i);
							str.append("{'lid':"+link.getId()+",");
							str.append("'linkName':'"+link.getLink_name()+"',");
							str.append("'linklist':[");
							List<Degree> degreeList=dao.getDegreeByLinkId(link.getId());
							if(degreeList.size()>0){
								for(int j=0;j<degreeList.size();j++){
									Degree degree=degreeList.get(j);
									str.append("{'degree':"+degree.getDegree_name()+",");
									str.append("'startTime':'"+degree.getStart_time_up()+":"+degree.getStart_time_down()+"',");
									str.append("'endTime':'"+degree.getEnd_time_up()+":"+degree.getEnd_time_down()+"'}");
									if(j<degreeList.size()-1){
										str.append(",");
									}
								}
							}
							str.append("]}");
							if(i<links.size()-1){
								str.append(",");
							}
						}
						str.append("]}");
					}else{
						str.append("{'success':0,'msg':'没有对应的生产排期!'}");
					}
				}
				return str.toString();
			} catch (DBException e) {
				e.printStackTrace();
			}
			
		}
//		String str="{'success':1,'msg':{'lid':12,'linkName':'link-name',";
//		str+="'linklist':[{'degree':123,'startTime':'2012-11-22','startTime':'2012-11-23'},";
//		str+="{'degree':124,'startTime':'2012-11-11','startTime':'2012-11-23'}]}}";
		return "{'success':0,'msg':'输入参数有误!'}";
	}
}
